#encoding: utf-8
class UsersController < ApplicationController
  # GET /users
  # GET /users.json
  def index
    logger.info(" index params======#{params}")
    login = params[:login].strip if params[:login]
    role_id = params[:user_role_id].strip if params[:user_role_id]
    @users = User.query_by_login_and_role_id(params[:page], login, role_id)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end

  # GET /users/new
  # GET /users/new.json
  def new
    @user = User.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

  # GET /users/1/edit
  def edit

    @user = User.find(params[:id])
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /users/1
  # PUT /users/1.json
  def update
    @user = User.find(params[:id])
    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :ok }
    end
  end

  #register user account
  # 货主注册
  def shipper_register
    @user = User.new
    respond_to do |format|
      format.html # shipper_register.html.erb
      format.json { render json: @user }
    end
  end

  # POST /users/create_shipper
  # 创建货主帐号
  def create_shipper
    params[:user][:role_id] = Role.find_by_code('101').id
    @user = User.new(params[:user])

    respond_to do |format|
      if params[:user][:acceptance] == "1" && @user.save
        #deliver_activation_email(@user)
        format.html { redirect_to "/shipper_register_success", :user_session =>{:login=> @user.login, :password => @user.password} }
      else
        format.html { render :action => "shipper_register" } # shipper_register.html.erb
      end
    end
  end

  def shipper_register_success
    @user_session = UserSession.new(params[:user_session])
    @user_session.save
    @user = current_user
  end

  # POST /users/check_email
  # 检查邮件是否已注册
  def check_email
    state = "fail" # 验证失败
    if params[:email]
      user = User.find_by_email(params[:email])
      if user
        notice = "该email已被使用，请使用别的email"
      else
        notice = "恭喜！该email可用"
        state = "success" # 验证成功
      end
    else
      notice = "请输入电子邮件地址"
    end

    render :json => {:notice => notice, :state => state}
  end

  # POST /users/check_login
  # 检查login name是否已注册
  def check_login
    state = "fail" # 验证失败
    if params[:login]
      user = User.find_by_login(params[:login])
      if user
        notice = "该用户名已被使用，请使用别的用户名"
      else
        notice = "恭喜！该用户名可用"
        state = "success" # 验证成功
      end
    else
      notice = "请输入您的用户名"
    end

    render :json => {:notice => notice, :state => state}
  end

  private
  def set_login_user
    User.login_name = current_user.login if current_user#登录名
    User.common_user = common_user?# true or false
    User.c_biz_unit_id  = current_user.biz_unit_id if current_user#企业id
    User.biz_unit_admin = biz_unit_admin?
    User.c_role_id =  current_user.role_id if current_user#role id
  end

  skip_before_filter :require_login, :only => [:shipper_register, :create_shipper, :check_email, :check_login]
  before_filter :set_login_user
  before_filter :clean_page, :only => [:index]
end